#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int port = 6789;

int main(int argc, char ** argv)
{
	int e;
	int socket_desc;
	int iter = 0;
	char buf[80];
	struct sockaddr_in address;

	bzero(&address, sizeof(address));
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = inet_addr("127.0.0.1");
	address.sin_port = htons(port);

	socket_desc = socket(AF_INET, SOCK_DGRAM, 0);
	e = connect(socket_desc, (struct sockaddr*)&address, sizeof(address));
	if (e != 0)
	{
		printf("connect to server errror: %d, but always send\n", e);
	}

	for (iter = 0; iter <= 20; iter++)
	{
		sprintf(buf, "data packet with ID %d\n", iter);
		sendto(socket_desc, buf, sizeof(buf), 0, (struct sockaddr*)&address, sizeof(address));
	}

	sprintf(buf, "quit\n");
	sendto(socket_desc, buf, sizeof(buf), 0, (struct sockaddr*)&address, sizeof(address));

	close(socket_desc);
	printf("All messages sent.\n");
	return 0;
}
